<?php
/**
 * 微信web模块和jsapi模块
 * User: 毛文科
 * Date: 2019/3/21 0021
 * Time: 9:56
 */
namespace mwechat\bind;
use mwechat\src\Curl;
use mwechat\src\MaoWeiXin;
use think\Exception;

class Wechat extends MaoWeiXin
{
    /**网页通过code获取openid和access_token
     * @param code $code
     * @param null获取的key $key
     * @return mixed|string
     * @throws \think\Exception
     */
    public function webCodeToOpenId($code,$key=null){
        $curl = new Curl();
        $appid = $this->getAppid();
        $secret = $this->getAppSecret();
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
        $array = $curl->fasong($url)->get();
        $array = json_decode($array,true);
        if($key){
            return $array[$key];
        }
        return $array;
    }

    /**网页获取code
     * @param null获取类型 $scope
     * @param null授权后回调地址 $redirect_uri
     * @param nullappid $appid
     * @param null参数 $state
     * @throws \think\Exception
     */
    public function webGetCode($scope=null,$redirect_uri=null,$appid=null,$state=null){
        if($redirect_uri){
            $this->setRedirectUri($redirect_uri);
        }
        if($appid){
            $this->setAppid($appid);
        }
        if(!$scope){
            $scope = 'snsapi_userinfo';
        }
        if(!$state){
            $state = '123';
        }
        $appid = $this->getAppid();
        $redirect_uri = $this->getRedirectUri();
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=$scope&state=$state#wechat_redirect";
        header("Location:".$url);
    }

    /**获取用户信息
     * @param null临时token $access_token
     * @param null $openid
     * @param null $lang
     * @return string
     * @throws \think\Exception
     */
    public function webGetUserInfo($access_token=null,$openid=null,$lang = null){
        if($access_token){
            $this->setAccessToken($access_token);
        }
        if($openid){
            $this->setOpenId($openid);
        }
        if(!$lang){
            $lang = 'zh_CN ';
        }
        $access_token = $this->getAccessToken();
        $openid = $this->getOpenId();
        $curl = new Curl();
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=$lang";
        $array = $curl->fasong($url)->get();
        return $array;
    }

    /**通过access_token获取jsapiticket
     * @param null $access_token
     * @return string
     * @throws \think\Exception
     */
    public function getJsapiTicket($access_token=null){
        if($access_token){
            $this->setAccessToken($access_token);
        }
        $access_token = $this->getAccessToken();
        $curl = new Curl();
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi";
        $array = $curl->fasong($url)->jsonToArray()->get();
        return $array['ticket'];
    }

    /**获取签名
     * @param 签名数据 $data
     * @param null签名类型 $type
     * @return string
     * @throws Exception
     */
    public function jsapiGetSign($data,$type=null){
        if(!$data){
            throw new Exception('签名没有数据');
        }
        if(!isset($data['noncestr'])||!$data['noncestr']){
            throw new Exception('没有设置随机字符串');
        }
        if(!isset($data['jsapi_ticket'])||!$data['jsapi_ticket']){
            throw new Exception('没有设置jsapi_ticket');
        }
        if(!isset($data['url'])||!$data['url']){
            throw new Exception('url');
        }
        if(!isset($data['timestamp'])||!$data['timestamp']){
           $data['timestamp'] = time();
        }
        if(!$type){
            $type = 'sha1';
        }
        ksort($data);
        $str = '';
        foreach ($data as $key=>$value){
            if(!$value){
                continue;
            }else{
                if(!$str){
                    $str = (string)$key."=$value";
                }else{
                    $str .= "&".$key."=$value";
                }
            }
        }
        return hash_hmac($type,$str);
    }

    /**获取jsapiaccesstoken
     * @return string
     * @throws Exception
     */
    public function getJsapiAccessToken(){
        $appid = $this->getAppid();
        $appsecret = $this->getAppSecret();
        $curl = new Curl();
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
        $array  = $curl->fasong($url)->jsonToArray()->get();
        return $array['access_token'];
    }

    /**微信小程序获取openid
     * @param $code
     * @param null $key
     * @return mixed|string
     * @throws Exception
     */
    public function codeGetProgram($code,$key=null){
        $curl = new Curl();
        $appid = $this->getAppid();
        $secret = $this->getAppSecret();
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        $array = $curl->fasong($url)->get();
        $array = json_decode($array,true);
        if($key){
            return $array[$key];
        }
        return $array;
    }
}